<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="__ADMIN__/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__ADMIN__/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <input type="hidden" id="hideCollectionId" value=""/>
    <input id="isReload" type="hidden" value="no">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-add-new">添加顶级节点</button>
            </div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn layui-btn-xs" lay-event="add">添加子权限</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="__ADMIN__/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="__ADMIN__/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="__ADMIN__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        var menuTree = function() {
            // 渲染表格
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'fid',
                elem: '#munu-table',
                url: '/admin/menu',
                page: false,
                cols: [[
                    {type: 'numbers'},
                    {field: 'name', minWidth: 200, title: '权限名称'},
                    {field: 'url', minWidth: 200, title: '菜单url'},
                    {field: 'hash', title: 'hash值'},
                    {field: 'sort', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'isMenu', 
                        width: 80, 
                        align: 'center', 
                        templet: function (d) {
                            if (d.is_menu == 1) {
                                return '<span class="layui-btn layui-btn-xs layui-btn-normal">菜单</span>';
                            } else {
                                return '<span class="layui-badge-rim">权限</span>';
                            }
                        }, 
                        title: '类型'
                    },
                    {
                        field: 'icon', 
                        width: 80, 
                        align: 'center', 
                        title: '图标',
                        templet: function (d) {
                            if(d.icon == '')
                                return ''
                            return '<i class="'+ d.icon +'"></i>';   
                        }
                    },
                    {field: 'status_text', width: 80, align: 'center', title: '状态',
                        templet: function (d) {
                            let color = '#e01010';
                            if(d.status == 1)
                                color = '#666';
                            return '<span style="color:'+ color +';">'+ d.status_text +'</span>';   
                        }
                    },
                    {templet: '#auth-state', width: 220, align: 'center', title: '操作'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        } 
        menuTree();
        // 添加顶级节点
        $('#btn-add-new').click(function(){
            addPage(0, '添加顶级权限', '/admin/menu/add');
            return false;
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if( layEvent === 'add' ) {
                addPage(data.id, '添加子权限', '/admin/menu/add');
            } else if (layEvent === 'del') {
                const title = '您是否要删除：' + data.name + '规则？'
                layer.confirm(title, function (index) {
                    $.ajax({
                        url: '/admin/menu/del/' + data.id,
                        method: 'delete',
                        success: function (res) {
                            layer.msg(res.msg, {
                                icon: res.code < 0 ? 2 : 1,
                                time: 500,
                                shade: 0.1
                            }, function () {
                                if(res.code > 0)
                                    obj.del();
                                layer.close(index);
                            });
                        }
                    })
                });
            } else if (layEvent === 'edit') {
                addPage(data.id, '修改规则', '/admin/menu/edit');
            }
        });

        function addPage(id = 0, title = '', url = '')
        {
            $('#hideCollectionId').val(id);
            $('#isReload').val('no');
            var index = layer.open({
                title: title,
                type: 2,
                shade: 0.2,
                shadeClose: true,
                area: ['100%', '100%'],
                content: url,
                end: function() {
                    const isReload = $('#isReload').val();
                    if(isReload == 'yes')
                        menuTree();
                }
            });
        }
    });
</script>
</body>
</html>